package com.rongzhe.house.ui.adapter;

import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.Dictionary;
import com.rongzhe.house.entity.voo.AppoiintmentInfo;
import com.rongzhe.house.manager.DictionaryManager;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by huang on 2018/1/4.
 */

public class AppointmentAdapter extends BaseAdapter {
    private List<AppoiintmentInfo> mDatas;
    private LayoutInflater mLayoutInflater;
    private Context mContext;

    public AppointmentAdapter(Context context) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setDatas(List<AppoiintmentInfo> datas) {
        mDatas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas == null ? null : mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView =  mLayoutInflater.inflate(R.layout.list_item_appointment, parent, false);
        }
        MyViewHolder holder = (MyViewHolder) convertView.getTag();
        if (holder == null) {
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        }
        final AppoiintmentInfo appoiintmentInfo = (AppoiintmentInfo) getItem(position);
        if(!TextUtils.isEmpty(appoiintmentInfo.getContactTime())) {
            try {
                Date date = new Date(Long.parseLong(appoiintmentInfo.getContactTime()));
                holder.textDate.setText(new SimpleDateFormat("yyyy-MM-dd").format(date));
            }catch (Exception e){
                Log.e(AppointmentAdapter.class.getSimpleName(), "error to create date : ", e);
            };
        }
        holder.textTimeSlot.setText(DictionaryManager.getInstance().pareItem(
                DictionaryManager.DICTIONARY_CONTACT_TIME_OPTION,
                appoiintmentInfo.getContactTimeOption()));
        holder.homeImg.setImageResource(R.drawable.banner_1);
        holder.textHouseTitle.setText(appoiintmentInfo.getHouseOverview().getName());
        //todo
        holder.textHouseFlow.setText("2/13层");
        holder.textHousePrice.setText(appoiintmentInfo.getHouseOverview().getRent().toBigInteger()
                + "元/月");
        return convertView;
    }

    class MyViewHolder {
        View root;
        @BindView(R.id.text_date)
        TextView textDate;
        @BindView(R.id.text_time_slot)
        TextView textTimeSlot;
        @BindView(R.id.home_img)
        AppCompatImageView homeImg;
        @BindView(R.id.text_house_title)
        TextView textHouseTitle;
        @BindView(R.id.text_house_flow)
        TextView textHouseFlow;
        @BindView(R.id.text_house_price)
        TextView textHousePrice;

        public MyViewHolder(View itemView) {
            root = itemView;
            ButterKnife.bind(this, itemView);
        }
    }
}
